<?php
namespace app\common\controller;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;
use app\user\controller\Login;

class CommonController extends Controller{
    public $USER_INFO=null;
    public function _initialize(){
        //清理header中缓存
        header("Pragma: no-cache");
        parent::_initialize();
        $this->UpdateToken();
    }
    /**
     * 验证是否登录
     *
     */
    public function UpdateToken(){
        $UserInfoArray = $this->USER_INFO;
        $Staff_Id = $UserInfoArray['Id'];
        $TokenExpire = Db::table('staff')
            ->field(
                [
                    'token_expire'=>'Expire'
                ]
            )
            ->where(
                [
                    'staff_id'=>$Staff_Id
                ]
            )
            ->find();
        if(!empty($TokenExpire) && $TokenExpire['Expire'] < time()){
            $Logout = new Login();
            $Logout->Logout();
        }else{
            $User = new Login();
            $this->USER_INFO = $User->USER_INFO;
            $UserToken = $User->GenerateToken($this->USER_INFO);
            Db::table('staff')
                ->where(
                   ['staff_id'=>$UserInfoArray['Id']]
                )
                ->update([
                    'token'=>$UserToken,
                    'token_expire'=>time() + (int)3600
                ]);
        }
    }

}